/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.help.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.help.model.HelpItem;
import org.crystalrange.help.service.HelpItemService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class HelpItemListController extends BaseController {

    protected HelpItemService helpItemService;
    protected HelpItem selectedHelpItem;
    protected Listbox listboxHelpItem;
    protected Search searchHelpItem;
    protected List<HelpItem> helpItems;

    public HelpItemService getHelpItemService() {
        return this.helpItemService;
    }

    public void setHelpItemService(HelpItemService helpItemService) {
        this.helpItemService = helpItemService;
    }

    public HelpItem getSelectedHelpItem() {
        return this.selectedHelpItem;
    }

    public void setSelectedHelpItem(HelpItem selectedHelpItem) {
        this.selectedHelpItem = selectedHelpItem;
    }

    public List<HelpItem> getHelpItems() {
        return this.helpItems;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchHelpItem = new Search(HelpItem.class);
        this.searchHelpItem.setMaxResults(100);
        this.searchHelpItem.addFetch("createdBy");
        this.searchHelpItem.addFetch("updatedBy");
        this.helpItems = this.helpItemService.search(this.searchHelpItem);

        component.setAttribute("controller", this, false);
    }

    public void onClick$menuitemHelpItemCreate(Event event) throws Exception {
        this.helpItemCreate();
    }

    public void onClick$menuitemHelpItemEdit(Event event) throws Exception {
        this.helpItemEdit();
    }

    public void onClick$menuitemHelpItemDelete(Event event) throws Exception {
        this.helpItemDelete();
    }

    public void onClick$menuitemHelpItemSearch(Event event) throws Exception {
        this.helpItemSearch();
    }

    public void onClick$menuitemHelpItemRefresh(Event event) throws Exception {
        this.helpItemRefresh();
    }

    public void onClick$toolbarbuttonHelpItemCreate(Event event) throws Exception {
        this.helpItemCreate();
    }

    public void onClick$toolbarbuttonHelpItemEdit(Event event) throws Exception {
        this.helpItemEdit();
    }

    public void onClick$toolbarbuttonHelpItemDelete(Event event) throws Exception {
        this.helpItemDelete();
    }

    public void onClick$toolbarbuttonHelpItemSearch(Event event) throws Exception {
        this.helpItemSearch();
    }

    public void onClick$toolbarbuttonHelpItemRefresh(Event event) throws Exception {
        this.helpItemRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.helpItemEdit();
    }
    
    private void helpItemCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/helpItem.zul");
    }

    private void helpItemEdit() throws Exception {
        if (this.selectedHelpItem != null) {
            this.execution.sendRedirect("~./zul/html/helpItem.zul?Id=" + this.selectedHelpItem.getId().toString());
        }
    }

    private void helpItemDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxHelpItem.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    HelpItem helpItem = (HelpItem) selectedItem.getValue();
                    this.helpItemService.remove(helpItem);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.helpItems = this.helpItemService.search(this.searchHelpItem);
        this.binder.loadAll();
    }

    private void helpItemSearch() throws Exception {
        this.execution.setAttribute("searchHelpItem", this.searchHelpItem);

        Window window = (Window) this.execution.createComponents("~./zul/html/helpItemSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchHelpItem = (Search) this.execution.getAttribute("searchHelpItem");
        }

        this.helpItems = this.helpItemService.search(this.searchHelpItem);
        this.binder.loadAll();
    }

    private void helpItemRefresh() {
        this.helpItems = this.helpItemService.search(this.searchHelpItem);
        this.binder.loadAll();
    }
}
